package com.yoomiito.app.utils.wx;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMMin;
import com.umeng.socialize.media.UMWeb;
import com.yoomiito.app.model.bean.UmShareInfo;

public class UMengShareUtils {

    public static void shareWX(Activity activity,String shareContent,String shareUrl,String shareTitle,String shareIcon){
        UMWeb web = new UMWeb(shareUrl);
        if (!TextUtils.isEmpty(shareTitle)){
            web.setTitle(shareTitle);//标题
        }
        if (!TextUtils.isEmpty(shareIcon)){
            web.setThumb(new UMImage(activity,shareIcon));  //缩略图
        }
        if (!TextUtils.isEmpty(shareContent)){
            web.setDescription(shareContent);//描述
        }

        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWX(Activity activity,String shareContent,String shareUrl,String shareTitle,int shareIconRes){
        UMWeb web = new UMWeb(shareUrl);
        if (!TextUtils.isEmpty(shareTitle)){
            web.setTitle(shareTitle);//标题
        }
        if (shareIconRes!=-1){
            web.setThumb(new UMImage(activity,shareIconRes));  //缩略图
        }
        if (!TextUtils.isEmpty(shareContent)){
            web.setDescription(shareContent);//描述
        }

        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWX(SHARE_MEDIA shareMedia,Activity activity,String shareContent,String shareUrl,String shareTitle,String shareIcon){
        UMWeb web = new UMWeb(shareUrl);
        if (!TextUtils.isEmpty(shareTitle)){
            web.setTitle(shareTitle);//标题
        }
        if (!TextUtils.isEmpty(shareIcon)){
            web.setThumb(new UMImage(activity,shareIcon));  //缩略图
        }
        if (!TextUtils.isEmpty(shareContent)){
            web.setDescription(shareContent);//描述
        }

        new ShareAction(activity)
                .setPlatform(shareMedia)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWXText(Activity activity,String shareContent){
        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withText(shareContent)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWXPic(SHARE_MEDIA shareMedia,Activity activity,String shareUrl){
        new ShareAction(activity)
                .setPlatform(shareMedia)//传入平台
                .withMedia(new UMImage(activity,shareUrl))
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

//    public static void shareWXPicText(SHARE_MEDIA shareMedia,String shareContent,Activity activity,String shareUrl){
//        new ShareAction(activity)
//                .setPlatform(shareMedia)//传入平台
//                .withText(shareContent)
//                .withMedia(new UMImage(activity,shareUrl))
//                .setCallback(mUMShareListener)//回调监听器
//                .share();
//    }

    public static void shareWXPic(SHARE_MEDIA shareMedia,Activity activity, Bitmap shareBitmap){
        new ShareAction(activity)
                .setPlatform(shareMedia)//传入平台
                .withMedia(new UMImage(activity,shareBitmap))
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

//    public static void shareWXCircle(Activity activity,String shareUrl){
//        new ShareAction(activity)
//                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
//                .withMedia(new UMImage(activity,shareUrl))
//                .setCallback(mUMShareListener)//回调监听器
//                .share();
//    }

    public static void shareWXCircle(Activity activity, String shareContent, String shareUrl, String shareTitle, String shareIcon){
        UMWeb web = new UMWeb(shareUrl);
        if (!TextUtils.isEmpty(shareTitle)){
            web.setTitle(shareTitle);//标题
        }
        if (!TextUtils.isEmpty(shareIcon)){
            web.setThumb(new UMImage(activity,shareIcon));  //缩略图
        }
        if (!TextUtils.isEmpty(shareContent)){
            web.setDescription(shareContent);//描述
        }

        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWXCircle(Activity activity, String shareContent, String shareUrl, String shareTitle, int shareIconRes){
        UMWeb web = new UMWeb(shareUrl);
        if (!TextUtils.isEmpty(shareTitle)){
            web.setTitle(shareTitle);//标题
        }
        if (shareIconRes!=-1){
            web.setThumb(new UMImage(activity,shareIconRes));  //缩略图
        }
        if (!TextUtils.isEmpty(shareContent)){
            web.setDescription(shareContent);//描述
        }

        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                .withMedia(web)
                .setCallback(mUMShareListener)//回调监听器
                .share();
    }

    public static void shareWXMiniProgram(Activity activity,UmShareInfo info){
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(activity).setShareConfig(config);
////兼容低版本的网页链接
        UMMin umMin = new UMMin(TextUtils.isEmpty(info.getShareUrl())?"":info.getShareUrl());
// 小程序消息封面图片
        if (!TextUtils.isEmpty(info.getImgUrl())){
            UMImage imagelocal = new UMImage(activity,info.getImgUrl());
            umMin.setThumb(imagelocal);
        }
// 小程序消息title
        if (!TextUtils.isEmpty(info.getTitle())){
            umMin.setTitle(info.getTitle());
        }
// 小程序消息描述
        if (!TextUtils.isEmpty(info.getDes())){
            umMin.setDescription(info.getDes());
        }
//小程序页面路径
        if (!TextUtils.isEmpty(info.getPath())){
            umMin.setPath(info.getPath());
        }
// 小程序原始id,在微信平台查询
        if (!TextUtils.isEmpty(info.getId())){
            umMin.setUserName(info.getId());
        }
        new ShareAction(activity)
                .withMedia(umMin)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .setCallback(mUMShareListener)
                .share();
    }

    private static UMShareListener mUMShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
//            YMToast.showText("分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
//            YMToast.showText("分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
//            YMToast.showText("取消分享");
        }
    };
}
